#include "download.h"

Download::Download( QUrl download_url, QString s, QString p )
{
this->setAttribute(Qt::WA_DeleteOnClose);
stopped = false;
purpose = s;
url = download_url;
path = p;
statusLabel = new QLabel( QLatin1String(""), this );
progressDialog = new QProgressDialog(this);
http = new QHttp(this);
buffer = new QBuffer(&ba, this);
progressDialog->setLabel(statusLabel);
QFileInfo fi( url.toString() );

    if ( true != url.isValid() ||
         true == url.host().isEmpty() )
    {
    return;
    }
    else
    {
    statusLabel->setText( fi.fileName() );
    }

buffer->open(QBuffer::ReadWrite);

    if ( url.port() == -1 )
    {
    http->setHost( url.host(), 80 );
    }
    else
    {
    http->setHost( url.host(), url.port() );
    }

http_request_id = http->get(url.path(), buffer);

QObject::connect( http,
                  SIGNAL( requestFinished(int, bool) ),
                  this,
                  SLOT( request_finished(int, bool) ) );
QObject::connect( http,
                  SIGNAL( dataReadProgress(int, int)),
                  this,
                  SLOT( update_progress(int, int) ) );
QObject::connect( http,
                  SIGNAL( responseHeaderReceived(const QHttpResponseHeader &) ),
                  this,
                  SLOT( read_response_header(const QHttpResponseHeader &) ) );
QObject::connect( progressDialog,
                  SIGNAL( canceled() ),
                  this,
                  SLOT( cancel_download() ) );

}

Download::~Download()
{
}

void Download::request_finished( int request_id , bool request_error )
{
    if ( true == stopped )
    {
    buffer->close();
    progressDialog->hide();
    return;
    }

    if ( http_request_id != request_id )
    {
    return;
    }
    
    if ( true == request_error )
    {
    stopped = true;
    buffer->close();
    progressDialog->hide();    
    emit download_finished( this, false, purpose, path, http->errorString() );
    }
    else
    {
    stopped = true;
    buffer->close();
    progressDialog->hide();    
    emit download_finished( this, true, purpose, path, QLatin1String("") );
    }
}

void Download::read_response_header( const QHttpResponseHeader & response_header )
{
    if ( true == stopped )
    {
    return;
    }

    if ( response_header.statusCode() != 200 )
    {
    stopped = true;
    progressDialog->hide();
    http->abort();
    emit download_finished( this, false, purpose, path, response_header.reasonPhrase() );
    }
}

QByteArray Download::return_data()
{
return ba;
}

void Download::update_progress( int read_bytes, int total_bytes )
{
    if ( true == stopped )
    {
    return;
    }
progressDialog->setMaximum(total_bytes);
progressDialog->setValue(read_bytes);
}

void Download::cancel_download()
{
statusLabel->setText(tr("Canceled."));
stopped = true;
http->abort();
buffer->close();
close();
}

void Download::closeEvent( QCloseEvent * e )
{
    if ( stopped == false )
    {
    stopped = true;
    http->abort();
    buffer->close();
    }
e->accept();
}
